package com.simple.cms.common.utils;

import java.util.Objects;
import java.util.function.Function;


public class NullSafety {
    public static <T> T valueOrDefault(T value, T defaultValue) {
        return value == null ? defaultValue : value;
    }

    public static <T, R> R valueOrDefault(T value, Function<T, R> accessor, R defaultValue) {
        return valueOrDefault(valueOrNull(value, accessor), defaultValue);
    }

    public static <T, R> R valueOrNull(T value, Function<T, R> accessor) {
        Objects.requireNonNull(accessor, "accessor cannot be null.");
        return value == null ? null : accessor.apply(value);
    }
}
